package no.ntnu.fp.model1;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;

public class lwsGui implements ActionListener {

	JFrame frame;
	LWS lws;
	int localWatchSystemId = 1;
	JButton MWS;
	int red, yellow, green;
	static boolean frameUp = false;
	JTextArea jtp;
	ArrayList<Well> wellList;
	mwsGui g;
	
	JButton checkButton, checkButton1, checkButton2, 
	checkButton3, checkButton4, checkButton5, 
	checkButton6, checkButton7, checkButton8, 
	checkButton9, checkButton10, checkButton11, checkButton12;
	
	public lwsGui(){
		
	}
	
	public lwsGui(LWS l){
		lws = l;
		localWatchSystemId = l.getLocalWatchSystemId();
	}
	
	private Color randomColor(){
		int i = (int) Math.round(Math.random()*3);
		Color reColor;
		if(i == 0){
			reColor = Color.red;
		}else if(i==2||i==1){
			reColor = Color.green;
		}else{
			reColor = Color.yellow;
		}
		//countColors(reColor);
		return reColor;
	}
	
	private void countColor(Color c){
		if(c==Color.red){
			red++;
		}else if(c==Color.yellow){
			yellow++;
		}else if(c==Color.green){
			green++;
		}
	}

	private JPanel makeColorPanel(){
		JPanel out = new JPanel(new GridLayout(0,3));
		JPanel jb = new JPanel();
		JPanel jb1 = new JPanel();
		JPanel jb2 = new JPanel();
		JPanel jb3 = new JPanel();
		JPanel jb4 = new JPanel();
		JPanel js = new JPanel();
		Color rColor = randomColor();
		countColor(rColor);
		jb.setBackground(rColor);
		js.setBackground(randomColor());
		jb1.setBackground(jb.getBackground());
		jb2.setBackground(jb.getBackground());
		jb3.setBackground(jb.getBackground());
		jb4.setBackground(jb.getBackground());

		Border blackline = BorderFactory.createLineBorder(Color.black);
		out.setBorder(blackline);
		js.setBorder(blackline);
		
		out.add(jb);
		out.add(jb1);
		out.add(js);
		out.add(jb2);
		out.add(jb3);
		out.add(jb4);
		
		
		return out;
	}
	
	private JPanel addPanel(String name, JButton b){
		TitledBorder title;
		JPanel panel = new JPanel(new GridLayout(0, 1));
		title = BorderFactory.createTitledBorder(name);
		panel.setBorder(title);
		
		JPanel j = makeColorPanel();
		b = new JButton("Check");
		b.addActionListener(this);
		panel.add(j);
		panel.add(b);
		return panel;
	}
	
	private JPanel initPanel(String well){
		TitledBorder title;
		JPanel panel = new JPanel(new GridLayout(0, 1));
		title = BorderFactory.createTitledBorder(well);
		panel.setBorder(title);
		JPanel j = makeColorPanel();
		panel.add(j);
		return panel;
	}
	
	protected void createGui(mwsGui g2){
		g=g2;
		//wellList = lws.getWellList();
		frame = new JFrame("WATCH");
		frame.setResizable(false);
		//frame.setUndecorated(true);
		frame.setLocation(100, 80);
		frame.setAlwaysOnTop(true);
		JPanel panel = new JPanel(new BorderLayout());
		TitledBorder title;
		title = BorderFactory.createTitledBorder("Local Watch System "+ localWatchSystemId);
		panel.setBorder(title);
		
		JPanel pInfo = new JPanel();
		JPanel subP = new JPanel(new GridLayout(0, 6));
		
		
		
		JPanel panel1 = initPanel("Well1");
		checkButton1 = new JButton("Check");
		checkButton1.addActionListener(this);
		panel1.add(checkButton1);
		subP.add(panel1);
		JPanel panel2 = initPanel("Well2");
		checkButton2 = new JButton("Check");
		checkButton2.addActionListener(this);
		panel2.add(checkButton2);
		subP.add(panel2);
		JPanel panel3 = initPanel("Well3");
		checkButton3 = new JButton("Check");
		checkButton3.addActionListener(this);
		panel3.add(checkButton3);
		subP.add(panel3);
		JPanel panel4 = initPanel("Well4");
		checkButton4 = new JButton("Check");
		checkButton4.addActionListener(this);
		panel4.add(checkButton4);
		subP.add(panel4);
		JPanel panel5 = initPanel("Well5");
		checkButton5 = new JButton("Check");
		checkButton5.addActionListener(this);
		panel5.add(checkButton5);
		subP.add(panel5);
		JPanel panel6 = initPanel("Well6");
		checkButton6 = new JButton("Check");
		checkButton6.addActionListener(this);
		panel6.add(checkButton6);
		subP.add(panel6);
		JPanel panel7 = initPanel("Well7");
		checkButton7 = new JButton("Check");
		checkButton7.addActionListener(this);
		panel7.add(checkButton7);
		subP.add(panel7);
		JPanel panel8 = initPanel("Well8");
		checkButton8 = new JButton("Check");
		checkButton8.addActionListener(this);
		panel8.add(checkButton8);
		subP.add(panel8);
		JPanel panel9 = initPanel("Well9");
		checkButton9 = new JButton("Check");
		checkButton9.addActionListener(this);
		panel9.add(checkButton9);
		subP.add(panel9);
		JPanel panel10 = initPanel("Well10");
		checkButton10 = new JButton("Check");
		checkButton10.addActionListener(this);
		panel10.add(checkButton10);
		subP.add(panel10);
		JPanel panel11 = initPanel("Well11");
		checkButton11 = new JButton("Check");
		checkButton11.addActionListener(this);
		panel11.add(checkButton11);
		subP.add(panel11);
		JPanel panel12 = initPanel("Well12");
		checkButton12 = new JButton("Check");
		checkButton12.addActionListener(this);
		panel12.add(checkButton12);
		subP.add(panel12);
		
		/**subP.add(addPanel("Well1", checkButton1));
		subP.add(addPanel("Well2", checkButton2));
		subP.add(addPanel("Well3", checkButton3));
		subP.add(addPanel("Well4", checkButton4));
		subP.add(addPanel("Well5", checkButton5));
		subP.add(addPanel("Well6", checkButton6));
		subP.add(addPanel("Well7", checkButton7));
		subP.add(addPanel("Well8", checkButton8));
		subP.add(addPanel("Well9", checkButton9));
		subP.add(addPanel("Well10", checkButton10));
		subP.add(addPanel("Well11", checkButton11));
		subP.add(addPanel("Well12", checkButton12));
		**/
		
		jtp = new JTextArea("Green: "+green+" Yellow: "+yellow+" Red: "+red); //Math.round((Math.random()*12))
		pInfo.add(jtp);
		panel.add(pInfo, BorderLayout.NORTH);
		panel.add(subP);
		MWS = new JButton("Back to Master Watch");
		MWS.addActionListener(this);
		panel.add(MWS, BorderLayout.SOUTH);
		
		frame.setContentPane(panel);
				
		frame.pack();
		frame.setSize(600, 400);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
	
	
	
	/**public static void main(String[] args) {
		new lwsGui().createGui();

	}**/

	@SuppressWarnings("deprecation")
	@Override
	public void actionPerformed(ActionEvent e) {
		//System.out.println("You pushed a button");
		int nr = 0;
		//System.out.println(e);
		if(e.getSource()==MWS){
			if(!frameUp){
				frame.hide();
				g.createGui();
			}else{System.out.println("Please close the well-frame before going back to Master Watch.");}
		}else if(e.getSource()==checkButton1){
			actionOutput(1);
		}else if(e.getSource()==checkButton2){
			actionOutput(2);
		}else if(e.getSource()==checkButton3){
			actionOutput(3);
		}else if(e.getSource()==checkButton4){
			actionOutput(4);
		}else if(e.getSource()==checkButton5){
			actionOutput(5);
		}else if(e.getSource()==checkButton6){
			actionOutput(6);
		}else if(e.getSource()==checkButton7){
			actionOutput(7);
		}else if(e.getSource()==checkButton8){
			actionOutput(8);
		}else if(e.getSource()==checkButton9){
			actionOutput(9);
		}else if(e.getSource()==checkButton10){
			actionOutput(10);
		}else if(e.getSource()==checkButton11){
			actionOutput(11);
		}else if(e.getSource()==checkButton12){
			actionOutput(12);
		}else{
			//frame.hide();
			if(!frameUp){
				frameUp = true;
				System.out.println("Loading well "+nr+"..");
				new wellGui().createGui();
			}else{System.out.println("A well-frame is already up. Close that before trying again.");}
		}
	}
	
	private void actionOutput(int i){
		if(!frameUp){
			frameUp = true;
			System.out.println("Loading well "+i+"..");
			new wellGui().createGui();
		}else{System.out.println("A well-frame is already up. Close that before trying again.");}
	}

	public static void setFrameUp(boolean b) {
		frameUp = b;
		if(!b){
			System.out.println("Well-frame closed.");
		}
	}

	public void setTextFelt(int green2, int yellow2, int red2) {
		jtp.setText("Green: "+green2+" Yellow: "+yellow2+" Red: "+red2);
	}

}
